package com.hua.springCloud.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
@Slf4j
public class FliterConfiguration implements GlobalFilter, Ordered{

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String username = exchange.getRequest().getQueryParams().getFirst("username");
        if("hua".equals(username)){
            return chain.filter(exchange);
        }
        log.info("用户名:{},错误,非法访问",username);
        exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
        return exchange.getResponse().setComplete();

    }

    //过滤器的权重，越小越先执行
    //    int HIGHEST_PRECEDENCE = -2147483648; 最小权重
    //    int LOWEST_PRECEDENCE = 2147483647;   最大权重
    @Override
    public int getOrder() {
        return 0;
    }
}
